package com.zhiwang.planet.util

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.os.StrictMode
import androidx.core.content.FileProvider
import android.util.Log
import com.zhiwang.planet.BuildConfig
import com.zhiwang.planet.mvvm.model.entity.ShareInformation
import com.zhiwang.planet.util.AppHelper.Helper.context
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException

@Deprecated("除了微博分享以外，已经改用 ShareHelper")
class ShareUtil {
    // 微信好友
    // package = com.tencent.mm,
    // activity = com.tencent.mm.ui.tools.ShareImgUI
    // 微信朋友圈
    // package = com.tencent.mm,
    // activity = com.tencent.mm.ui.tools.ShareToTimeLineUI
    //
    // package = com.tencent.mobileqq,
    // activity = com.tencent.mobileqq.activity.JumpActivity
    // package = com.tencent.mobileqq,
    // activity = com.tencent.mobileqq.activity.qfileJumpActivity
    // QQ空间
    // package = com.qzone, activity =
    // com.qzonex.module.operation.ui.QZonePublishMoodActivity
    // 人人
    // package = com.renren.mobile.android,
    // activity = com.renren.mobile.android.publisher.UploadPhotoEffect
    // 陌陌
    // package = com.immomo.momo, activity =
    // com.immomo.momo.android.activity.feed.SharePublishFeedActivity
    // 新浪微博
    // package = com.sina.weibo, activity = com.sina.weibo.EditActivity

    companion object ShareHelperSOUL {

        private fun saveBitmap(bm: Bitmap, picName: String): String {
            try {
                val path = context.cacheDir.absolutePath + File.separator + picName
                val f = File(path)
                if (!f.exists()) {
                    f.parentFile.mkdirs()
                    f.createNewFile()
                }

                val out = FileOutputStream(f)
                bm.compress(Bitmap.CompressFormat.PNG, 90, out)
                out.flush()
                out.close()
                Log.i("999", "保存成功：path=$path")
                return f.absolutePath
            } catch (e: FileNotFoundException) {
                e.printStackTrace()
            } catch (e: IOException) {
                e.printStackTrace()
            }

            return ""
        }

        fun isInstalled(context: Context, packageName: String, activityName: String): Boolean {

            val packages = context.packageManager.getInstalledPackages(0)
            packages.forEach {
                if (it.packageName.equals(packageName))
                    return true
            }
            return false
        }

        fun shareToWeChatFriend(context: Context, file: File) {
            share(false, context, file, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI")
        }

        fun shareToWeChatTimeLine(context: Context, file: File) {
            share(false, context, file, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI")
        }

        fun shareToQQFriend(context: Context, file: File) {
            if (isInstalled(context, "com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity"))
                share(context, file, "com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity")
            else {//尝试tim分享
                if (isInstalled(context, "com.tencent.tim", "com.tencent.mobileqq.activity.JumpActivity"))
                    share(context, file, "com.tencent.tim", "com.tencent.mobileqq.activity.JumpActivity")
            }
        }

        fun shareToQQZone(context: Context, file: File) {
            share(context, file, "com.qzone", "com.qzonex.module.operation.ui.QZonePublishMoodActivity")
        }

        fun shareToWeibo(context: Context, shareInformation: ShareInformation) {
            var pn = "com.sina.weibo"
            if (!isInstalled(context, pn, "")) {
                pn = "com.weico.international"
                if (!isInstalled(context, pn, "")) {
                    Toaster.message("未安装该应用")
                    return
                }
            }
            val intent = Intent()

            var uri = Uri.fromFile(File(shareInformation.imgLocalPath))
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                if (false) {
//                    intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
//                    uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file)
//                } else {
                StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder().build())
//                }
            }

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.putExtra(Intent.EXTRA_STREAM, uri)
            intent.action = Intent.ACTION_SEND
            intent.type = "image/*"
//            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file))
            intent.putExtra(Intent.EXTRA_TEXT, "${shareInformation.summary} ${shareInformation.url}")
            intent.`package` = pn
            context.startActivity(intent)
        }


        fun share(b: Boolean, context: Context, file: File, packageName: String, componentName: String) {
            if (!isInstalled(context, packageName, componentName)) {
                Toaster.message("未安装该应用")
                return
            }
            val intent = Intent()

            var uri = Uri.fromFile(file)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                if (b) {
                    intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
                    uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file)
                } else {
                    StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder().build())
                }
            }

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.putExtra(Intent.EXTRA_STREAM, uri)
            intent.putExtra(Intent.EXTRA_TEXT, "Hello Lady")
            intent.action = Intent.ACTION_SEND
            intent.type = "image/*"
//            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file))
//            intent.putExtra(Intent.EXTRA_TEXT, "分享下载吧")
            intent.component = ComponentName(packageName, componentName)
            context.startActivity(intent)
        }


        fun share(context: Context, file: File, packageName: String, componentName: String) {
            share(false, context, file, packageName, componentName)
        }

    }
}